package net.mikoo.seals.api.controller;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import net.mikoo.seals.api.model.ResultType;
import net.mikoo.seals.api.model.ota.CheckUpdateRequest;
import net.mikoo.seals.api.model.ota.CheckUpdateResponse;
import net.mikoo.seals.biz.service.DevicesServices;
import net.mikoo.seals.biz.service.OTAService;
import net.mikoo.seals.model.DevicesModel;

@Controller
public class OTAController {

	private final static Logger logger = LoggerFactory.getLogger(OTAController.class);
	
	@Autowired
	public DevicesServices devicesServices;
	
	@Autowired
	private OTAService otaService;
	
	@RequestMapping("/ota/checkUpdate")
	@ResponseBody
	public CheckUpdateResponse checkUpdate(CheckUpdateRequest request) {
		CheckUpdateResponse result = new CheckUpdateResponse();
		try {
			if(request == null) {
				throw new IllegalArgumentException("request");
			}
			
			if(request.getDeviceId() == null) {
				throw new IllegalArgumentException("request.deviceId");
			}
			
			DevicesModel model = otaService.checkUpdate(request.getDeviceId());
			result = CheckUpdateResponse.valueOf(model);
			result.setResult(ResultType.SUCCESS.getValue());
		}catch(Exception e) {
			logger.error("", e);
			result.setResult(ResultType.ERROR.getValue());
		}
		return result;
	}
	
	
}
